package com.htfc786.reminder.controller;

import com.htfc786.reminder.annotation.AuthCheck;
import com.htfc786.reminder.common.BaseResponse;
import com.htfc786.reminder.common.ResponseUtils;
import com.htfc786.reminder.model.dto.family.FamilyMemberRequest;
import com.htfc786.reminder.model.dto.family.FamilyRequest;
import com.htfc786.reminder.model.entity.Family;
import com.htfc786.reminder.model.entity.FamilyMember;
import com.htfc786.reminder.model.entity.User;
import com.htfc786.reminder.service.FamilyService;
import com.htfc786.reminder.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/family")
public class FamilyController {

    @Resource
    private UserService userService;

    @Autowired
    private FamilyService familyService;

    @PostMapping
    @AuthCheck
    public BaseResponse<Family> createFamily(@RequestBody FamilyRequest familyRequest, HttpServletRequest request) {
        Long userId = userService.getLoginUserId(request);
        Family family = familyService.createFamily(familyRequest.getName(), userId);
        return ResponseUtils.success(family);
    }

    @PutMapping
    @AuthCheck
    public BaseResponse<Family> updateFamilyName(@RequestParam String name, HttpServletRequest request) {
        Long userId = userService.getLoginUserId(request);
        User user = userService.getById(userId);
        Family family = familyService.updateFamilyName(user.getFamilyId(), name);
        return ResponseUtils.success(family);
    }

    @GetMapping
    public BaseResponse<Family> getFamilyById(@RequestParam Long id) {
        return ResponseUtils.success(familyService.getFamilyById(id));
    }

    @GetMapping("/search")
    public BaseResponse<List<Family>> searchFamiliesByName(@RequestParam String name) {
        return ResponseUtils.success(familyService.searchFamiliesByName(name));
    }

    @PostMapping("/member")
    @AuthCheck
    public BaseResponse<FamilyMember> addFamilyMember(@RequestParam String name, HttpServletRequest httpRequest) {
        Long userId = userService.getLoginUserId(httpRequest);
        User user = userService.getById(userId);
        FamilyMember familyMember = familyService.addFamilyMember(user.getFamilyId(), name);
        return ResponseUtils.success(familyMember);
    }

    @DeleteMapping("/member")
    @AuthCheck
    public BaseResponse<String> deleteFamilyMember(@RequestParam Long familyMemberId, HttpServletRequest httpRequest) {
        Long userId = userService.getLoginUserId(httpRequest);
        familyService.deleteFamilyMember(familyMemberId, userId);
        return ResponseUtils.success("ok");
    }

    @PutMapping("/member")
    @AuthCheck
    public BaseResponse<FamilyMember> updateFamilyMember(@RequestBody FamilyMemberRequest request, HttpServletRequest httpRequest) {
        Long userId = userService.getLoginUserId(httpRequest);
        FamilyMember old = FamilyMemberRequest.to(request);
        FamilyMember familyMember = familyService.updateFamilyMember(old, userId);
        return ResponseUtils.success(familyMember);
    }

    @GetMapping("/member")
    @AuthCheck
    public BaseResponse<List<FamilyMember>> getAllFamilyMembers(HttpServletRequest httpRequest) {
        Long userId = userService.getLoginUserId(httpRequest);
        User user = userService.getById(userId);
        List<FamilyMember> familyMembers = familyService.getAllFamilyMembers(user.getFamilyId());
        return ResponseUtils.success(familyMembers);
    }
}